# -*- coding: utf-8 -*-

class Control
  
  def initialize(name)
    
    @ver = "0.00.16"
  
    @params = {}
    @session = {}
    @captions = {}
    
    init_model(name)
    customize_model()
    
    init_view()
    customize_view()
    
    @title = name
    @menu = ""
    @contents = ""
    
  end
  
  def terminate()
    @mdl.close()
  end
  
  def init_model(name)
    @mdl = Model.new(name)
  end
  
  def customize_model()
  end
  
  def init_view()
    @vw = View.new()
  end
  
  def customize_view()
  end
  
  def count()
    return @mdl.count()
  end
  
  def count_by_field(field, value)
    return @mdl.count_by_column(field, value)
  end
  
  def set_params(name, value)
    @params[name] = value
  end
  
  def get_params(name)
    return @params[name]
  end
  
  def output(html)
    
    #@title += " [#{@ver}]"
    #html << "<div align='right' style='clear:both;'>#{@ver}</div>"
    
    @contents = html
    #@contents += print_params()
    values = { "title" => @title, "menu" => @menu, "contents" => @contents }
    values = add_page_values(values)
    
    print @vw.load(values, $templete_dir + "page_main.html")
    #print values.to_s
    
    terminate()
    
  end
  
  def set_params()
  
    if $cgi.request_method == "POST" then
      $cgi.params.each {|key, val|
        @params[key] = val.to_s
        #print "<div>#{key} = #{val}</div>"
      }
    end
    
  end
  
  def html_open()
    
    set_params()
=begin
    if $cgi.request_method == "POST" then
      $cgi.params.each {|key, val|
        @params[key] = val.to_s
        #print "<div>#{key} = #{val}</div>"
      }
    end
=end
    
    print $cgi.header()
=begin
    if $print_header then
      #print "HTTP/1.1 200 OK\n"
      print "Content-type: text/html\n"
      print "Set-Cookie: _session_id=#{$session.get_session_id}; path=/#{$html_dir}\n"
      print "\n"
    else
      print $cgi.header(
        {
          "type"=>"text/html",
          "Set-Cookie"=>"_session_id=#{$session.get_session_id}; path=/#{$html_dir}"
        }
      )
    end
=end
    
  end
  
  def html_close()
    $session.close()
  end
  
  def print_params()
    html = "<p>PARAMS</p>"
    
    html += "<p>POST</p>"
    $_POST.each {|key,val|
      html += "<p>#{key} | #{val}</p>"
    }
    
=begin
    html += "<p>POST+</p>"
    $cgi.params.each {|key, val|
      @params[key] = val.to_s
      html += "<p>#{key} | #{val}</p>"
    }
=end
    
    html += "<p>GET</p>"
    $_GET.each {|key,val|
      html += "<p>#{key} | #{val}</p>"
    }
    
    return "<div style='margin-top: 20px;'>#{html}</div>"
    
  end
  
  def main()
    
    html_open()
    
    mode = @params["mode"]
    id = @params["id"]
    html = make_page_contents(mode, id)
    
    output(html)
    
    html_close()
    
  end
  
  def add_page_values(values)
    return values
  end
  
  def make_page_contents(mode, id)
  
    case mode.to_s
    when "view"
      html = make_view_page(id)
    when "edit"
      html = make_edit_page(id)
    when "reedit"
      html = make_reedit_page()
    when "conf"
      html = make_conf_page()
    when "apply"
      html = make_apply_page()
    when "find"
      html = make_find_page()
    when "delete"
      html = make_delete_page(id)
    when "del_conf"
      html = make_del_conf_page(id)
    else
      html = customize_main(mode, id)
    end
    
    #html = "123" + html
    #print "Control - make_page_contents()"
    
    return html
    
  end
  
  def customize_main(mode, id)
    return make_list_page()
  end
  
#--- View ---
  def make_view_page(id)
    values = add_type_to_value(@mdl.get_data_by_id(id))
    values = customize_view_values(values)
    return show_view_form(values)
  end
  
  def customize_view_values(values)
    return values
  end
  
  def show_view_form(values)
    return @vw.make_view_form_with_edit(values)
  end
  
#--- Edit & ReEdit ---
  def make_edit_page(id)
    values = @mdl.get_data_by_id(id)
    return make_edit_page_by_values(values)
  end
  
  def make_reedit_page()
    values = @params
    return make_edit_page_by_values(values)
  end
  
  def make_edit_page_by_values(values)
    #values = add_type_for_edit_value(values)
    values = add_type_to_value(values)
    values = customize_edit_values(values)
    #values["mode"] = { "type" => "hidden", "val" => "conf" }
    values["mode"] = set_edit_value("hidden", "mode", "conf")
    return show_edit_form(values)
  end
  
  #def add_type_for_edit_value(values)
  def add_type_to_value(values)
    values.each {|key, val|
      case val
      when "id"
        values[key] = set_edit_value("hidden", key, val)
        #values[key] = { "type" => "hidden", "caption" => key, "val" => val }
      else
        values[key] = set_edit_value("text", key, val)
        #values[key] = { "type" => "text", "caption" => key, "val" => val }
      end
    }
    
    @captions.each {|key, val|
      if values.has_key?(key) then
        values[key]["caption"] = val
      end
    }
    
    return values
  end
  
  def set_edit_value(type, caption, value)
    return {
      "type" => type,
      "caption" => caption,
      "val" => value
    }
  end
  
  def customize_edit_values(values)
    return values
  end
  
  def show_edit_form(values)
    return @vw.make_edit_form(values)
  end
  
#--- Confirmation for Edit ---
  def make_conf_page()
    values = add_type_to_value(@params)
    values = customize_conf_values(values)
    values["mode"] = "apply"
    
=begin
    html = ""
    values.each { |row|
      html += row.to_s + "<br>"
    }
    return html
=end
    
    return show_conf_form(values)
  end
  
  def customize_conf_values(values)
    return values
  end
  
  def show_conf_form(values)
    return @vw.make_conf_form(values)
  end
  
#--- Apply ---
  def make_apply_page()
    
    values = customize_apply_values(@params)
    id = @mdl.apply(values)
    return @vw.make_apply_form(values)
    
  end
  
  def customize_apply_values(values)
    return values
  end
  
#--- Find ---
  def make_find_page()
    return get_find_list()
  end
  
  def get_find_list()
    
    values = get_find_values()
    values = customize_list_values(values)
    headers = customize_list_headers()
    return customize_list_table(headers, values)
    
  end
  
  def get_find_values()
    values = @mdl.get_data_by_word(@params["column"], @params["word"])
    return values
  end
  
#--- Delete ---
  def make_delete_page(id)
    
    @mdl.delete(id)
    return make_list_page()
    
  end
  
#--- Confirmation for Delete ---
  def make_del_conf_page(id)
    values = add_type_to_value(@mdl.get_data_by_id(id))
    values = customize_view_values(values)
    return show_delete_form(values)
  end
  
  def show_delete_form(values)
    return @vw.make_view_form_with_delete(values)
  end
  
#--- List ---
  def make_list_page()
    html = get_new_form()
    html += make_list()
    return html
  end
  
  def make_list()
    values = get_list_values()
    values = customize_list_values(values)
    headers = customize_list_headers(self.get_list_headers())
    return customize_list_table(headers, values)
  end
  
  def get_list_values()
    return @mdl.get_data_with_order("id")
  end
  
  def customize_list_values(values)
    return values
  end
  
  def get_list_headers()
    
    fields = @mdl.get_fields()
    
    headers = []
    fields.each { |fd|
      headers << fd
    }
    return headers
    
  end
  
  def customize_list_headers(headers)
    return headers
  end
  
  def customize_list_table(fields, values)
    
    html = ""
    #html = @vw.get_new_form()
    
    headers = []
    i = 0
    fields.each {|key|
      if @captions.has_key?(key) then
        headers[i] = @captions[key]
      else
        headers[i] = key
      end
      i += 1
    }
    
    html += @vw.make_list_table(headers, fields, values)
    return html
  end
  
#---
  def get_data_by_id(id, column)
    values = @mdl.get_data_by_id(id)
    return values[column]
  end
  
  def get_field_by_id(id, field)
    value = @mdl.get_column_by_id(id, field)
    return value
  end
  
#--- Make HTML Code ---
  def get_select_form_by_field(name, field, default)
    values = @mdl.get_column(field)
    html = make_select_form_without_key(name, values, default)
    return html
  end
  
  def get_select_form_with_id_by_field(name, field, id)
    values = @mdl.get_column_with_id(field)
    wk = {}
    values.each { |val|
      wk[val["id"]] = val[field]
    }
    html = make_select_form(name, wk, id.to_s)
    return html
  end
  
  def get_select_form_with_id_and_order_by_field(name, field, id, order)
    values = @mdl.get_column_with_id_and_order(field, order)
    wk = []
    i = 0
    values.each { |val|
      wk[i] = { "id" => val["id"], "val" => val[field]}
      i+=1
    }
    html = make_select_form_by_array(name, wk, id.to_s)
    return html
  end
  
  def get_select_form_with_id_and_status_and_order_by_field(name, field, id, status, order)
    values = @mdl.get_column_with_id_and_status_and_order(field, status, order)
    wk = []
    i = 0
    values.each { |val|
      wk[i] = { "id" => val["id"], "val" => val[field]}
      i+=1
    }
    html = make_select_form_by_array(name, wk, id.to_s)
    return html
  end
  
  def get_new_form()
    return @vw.get_new_form()
  end
  
  def get_find_form(field, word)
    return @vw.make_find_form(field, word)
  end
  
  def params_list()
    html = ""
    @params.each{|key, val|
      html += "<div>#{key} : #{val}</div>"
    }
    return html
  end
  
  def export()
    
=begin
    $cgi.params.each {|key, val|
      @params[key] = val.to_s
      #print "<div>#{key} = #{val}</div>"
    }
    print $cgi.header()
=end
    
    html_open()
    
    mode = @params["mode"]
    id = @params["id"]
    html = make_page_contents(mode, id)
    
    print html
    
  end
  
end
